<template>
    <div>
        <!--操作栏-->
        <div style="margin: 10px 0;">
            <!--上传-->
            <el-popover
                    placement="right"
                    width="400"
                    trigger="click"
            >
                <el-button slot="reference" type="primary" style="margin-right: 10px">上传文件<i class="el-icon-top"></i>
                </el-button>
                <el-upload
                        accept="jpg"
                        action="http://localhost:9090/files/upload"
                        :on-success="handleImportSuccess"
                        :on-error="handleImportError"
                        multiple
                        :limit="3"
                        :file-list="fileList"
                        :auto-upload="false"
                        ref="upload">
                    <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
                    <el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器
                    </el-button>
                    <div slot="tip" class="el-upload__tip">只能上传图片文件</div>
                </el-upload>
            </el-popover>
            <el-button type="danger" @click="delBatch" style="margin-right: 10px">批量删除<i
                    class="el-icon-remove-outline"></i></el-button>

        </div>
        <!--表格-->
        <el-table :data="tableData" :border="true" height="400" stripe
                  :header-cell-style="{background:'#eee'}"
                  ref="multipleTable"
                  @selection-change="handleSelectionChange"
        >
            <el-table-column
                    type="selection"
                    width="55">
            </el-table-column>
            <el-table-column prop="id" label="ID" width="70" fixed="left">
            </el-table-column>
            <el-table-column prop="name" label="文件名称">
            </el-table-column>
            <el-table-column prop="type" label="文件类型" width="120">
            </el-table-column>
            <el-table-column prop="size" label="文件大小(KB)" width="120">
            </el-table-column>
            <el-table-column label="下载" width="120">
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            type="success"
                            @click="download(scope.row.url)">下载 <i class="el-icon-edit"></i>
                    </el-button>
                </template>
            </el-table-column>
            <el-table-column prop="enable" label="启用">
                <template slot-scope="scope">
                    <el-switch
                            v-model="scope.row.enable"
                            active-color="#13ce66"
                            inactive-color="#ff4949" @change="changeEnable(scope.row)">
                    </el-switch>
                </template>
            </el-table-column>
            <el-table-column label="操作" >
                <template slot-scope="scope">
                    <el-button
                            size="mini"
                            type="danger"
                            @click="handleDelete(scope.row)">删除 <i
                            class="el-icon-remove-outline"></i></el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页控件 -->
        <div class="block">
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="pageNum"
                    :page-sizes="[2, 5, 10, 20]"
                    :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="total">
            </el-pagination>
        </div>
    </div>
</template>

<script>
    export default {
        name: "File",
        data() {
            return {
                //分页
                pageNum: 1,
                pageSize: 5,
                total: 0,

                tableData: [],
                fileList: [],
                multipleSelection: [],
                name: '',
            }
        },
        created() {
            this.getAll();
        },
        methods: {
            //分页
            getAll() {
                this.request.get("/files/page", {
                    params: {
                        pageNum: this.pageNum,
                        pageSize: this.pageSize,
                        name: this.name,
                    }
                }).then((res) => {
                    this.tableData = res.data.records;
                    this.total = res.data.total;
                }).catch(res => {
                });
            },
            //清楚查询消息
            clearQuery() {
                this.user.username = "",
                    this.user.nickname = "",
                    this.user.address = "",
                    this.getAll()
            },
            //删除
            handleDelete(row) {
                this.$confirm("此操作永久删除当前信息，是否继续？", "提示", {type: "info"}).then(() => {//确认删除
                    this.request.delete("/files/" + row.id).then(res => {
                        if (res.flag) {
                            this.$message.success("删除成功");
                        } else {
                            this.$message.error("数据同步失败，自动刷新");
                        }
                    }).finally(() => {
                        this.getAll();
                    });
                }).catch(() => {//取消删除
                    this.$message.info("取消操作");
                });
            },

            handleSelectionChange(val) {
                this.multipleSelection = val;//对象数组
            },
            //删除选中
            delBatch() {
                let ids = this.multipleSelection.map(value => value.id);//转化为id数组
                this.$confirm("此操作永久删除当前信息，是否继续？", "提示", {type: "info"}).then(() => {
                    this.request.post("/files/del/batch", ids).then(res => {
                        if (res.flag) {
                            this.$message.success("删除成功");
                        } else {
                            this.$message.error("数据同步失败，自动刷新");
                        }
                    }).finally(() => {
                        this.getAll();
                    })
                }).catch(() => {
                    this.$message.info("取消操作");
                })
            },
            handleSizeChange(val) {
                this.pageSize = val;
                this.getAll();
            },
            handleCurrentChange(val) {
                this.pageNum = val;
                this.getAll();
            },

            //导入成功
            handleImportSuccess(response, file, fileList) {
                console.log("re:" + response)//url
                this.fileList.push(file)
                if (response) {
                    this.$message.success("导入成功");
                } else {
                    this.$message.error("导入失败");
                }
                this.getAll();
            },
            //导入失败
            handleImportError(err,file,fileList){
                console.log(err,file,fileList);
                this.$message.error("文件过大，上传失败")
            },
            //上传选中文件
            submitUpload() {
                this.$refs.upload.submit();
            },
            /*handleRemove(file, fileList) {
                //移除已上传文件
                console.log("2:" + file, fileList);
            },
            handlePreview(file) {
                //点击已上传文件
                console.log("handlePreview:" + file);
            },
            handleExceed(files, fileList) {
                this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
            },
            beforeRemove(file, fileList) {
                //上传之前移除
                console.log("1:" +file,fileList);
                /!*return this.$confirm(`确定移除 ${file.name}？`, "提示", {type: "info"}).then(()=>{

                }).catch(()=>{
                    this.$message.info("取消操作");
                });*!/
            },*/
            //下载
            download(url) {
                window.open(url);
            },
            //启用
            changeEnable(file){
                this.request.post("/files/update",file).then(res=>{
                    if (res.flag){
                        this.$message.success("操作成功");
                    }else {
                        this.$message.error("操作失败");
                    }
                })
            },
        }
    }
</script>

<style scoped>

</style>